package com.yenlien.traceyun.webase.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;

import com.yenlien.traceyun.webase.exception.BusinessException;


//@Controller
//@RequestMapping("/error")
public class ErrorPageController extends AbstractErrorController {


    private ErrorAttributes errorAttributes;

    public ErrorPageController(ErrorAttributes errorAttributes) {
        super(errorAttributes);
        this.errorAttributes = errorAttributes;
    }

    private static final String ERROR_PATH = "/error";


    @RequestMapping
    public void error(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        WebRequest webRequest = new ServletWebRequest(request);
        Throwable error = errorAttributes.getError(webRequest);
        Integer statusCode = (Integer) request
                .getAttribute("javax.servlet.error.status_code");
        if (statusCode == 404) {
            throw new BusinessException("404", "404页面不存在");
        } else if (statusCode == 500) {
            throw error;
        } else {
            throw new BusinessException("" + statusCode, "http错误状态码：" + statusCode + " ");
        }

    }

    public String getErrorPath() {
        return ERROR_PATH;
    }

}
